package com.jsb.laundry.controller;

import com.alibaba.fastjson.JSONObject;
import com.jsb.laundry.common.enums.ReturnStatusCode;
import com.jsb.laundry.common.response.WebResponse;
import com.jsb.laundry.model.dto.QuestionBankRequest;
import com.jsb.laundry.model.vo.QuestionBankVo;
import com.jsb.laundry.service.RiderUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Api(tags = {"答题"})
@RestController
@RequestMapping("/questionBank/")
public class QuestionBankController {
    @Resource
    private RiderUserInfoService userInfoService;

    @ApiOperation(value = "答题列表", notes = "答题列表", httpMethod = "POST")
    @PostMapping("question")
    public WebResponse question() {
        List<QuestionBankVo> byList = userInfoService.findByList();

        return WebResponse.successData(byList);
    }
    @ApiOperation(value = "查询分数", notes = "查询分数", httpMethod = "POST")
    @PostMapping("questionNumber")
    public WebResponse questionNumber(@RequestBody QuestionBankRequest questionBankRequest) {
      int number =   userInfoService.questionNumber(questionBankRequest.getUserId());
        HashMap map = new HashMap();

      if (number<99){
          map.put("msg",ReturnStatusCode.ERRORSRIDER.getMsg());
          map.put("number",number);
          return WebResponse.successData(map);
      }
      if (number==100){
          map.put("msg",ReturnStatusCode.OK.getMsg());
          map.put("number",number);
          return WebResponse.successData(map);
      }
      return WebResponse.successData(null);
    }
    @ApiOperation(value = "交卷", notes = "交卷", httpMethod = "POST")
    @PostMapping("updQuestion")
    public WebResponse updQuestion(@RequestBody QuestionBankRequest questionBankRequest) {

        int byQuestionCount = userInfoService.findByQuestionCount(questionBankRequest.getUserId());
        if (byQuestionCount>=5){
            return WebResponse.success(ReturnStatusCode.MAXIMUM_NUMBER_OF_EXAMS.getCode(), ReturnStatusCode.MAXIMUM_NUMBER_OF_EXAMS.getMsg());
        }
        List<QuestionBankVo> byList = userInfoService.findByLists();
        int num = 0;
        List<QuestionBankRequest.AnswerList> answerList = questionBankRequest.getAnswerList();

        for (QuestionBankRequest.AnswerList list : answerList) {
            for (QuestionBankVo questionBankVo : byList) {

                if (list.getQuestionID().equals(questionBankVo.getId()) && list.getAnswer().equals(questionBankVo.getAnswer())) {
                    num += questionBankVo.getFraction();
                }
            }

        }
        userInfoService.addLog(questionBankRequest.getUserId(), String.valueOf(num));

        return WebResponse.successData(num);
    }
    @ApiOperation(value = "考试次数", notes = "考试次数", httpMethod = "POST")
    @PostMapping("findByQuestionCount")
    public WebResponse findByQuestionCount(@RequestBody JSONObject object){
        String userId = object.getString("userId");
        int byQuestionCount = userInfoService.findByQuestionCount(userId);
        int number = 5;
        return WebResponse.successData(number-byQuestionCount);
    }
    @ApiOperation(value = "考试公告", notes = "考试公告", httpMethod = "POST")
    @PostMapping("findByQuestionInfo")
    public WebResponse findByQuestionInfo(){
        String byQuestionInfo = userInfoService.findByQuestionInfo();
        return WebResponse.successData(byQuestionInfo);
    }
}
